package com.skinchange;

import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.skinchange.base.BaseSkinActivity;
import com.skinchange.skin.ResourcesManager;

import java.io.File;
import java.lang.reflect.Method;

//我更改了内容
public class MainActivity extends BaseSkinActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mListView;
    private String[] mDatas = new String[]{"Activity","Service","Activity","Service","Activity","Service"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initEvent() {

        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1-slideOffset;
                float rightScale = 0.8f + scale * 0.2f;

                if (drawerView.getTag().equals("LEFT")){

                    float leftScale = 1 - 0.3f * scale;

                    ViewHelper.setScaleX(mMenu,leftScale);
                    ViewHelper.setScaleY(mMenu,leftScale);
                    ViewHelper.setAlpha(mMenu,06f + 0.4f * (1-scale));
                    ViewHelper.setTranslationX(mContent,mMenu.getMeasuredWidth() * (1-scale));
                    ViewHelper.setPivotX(mContent,0);
                    ViewHelper.setPivotY(mContent,mContent.getMeasuredHeight()/2);

                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent,rightScale);
                    ViewHelper.setScaleY(mContent,rightScale);
                }
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.id_left_menu_container);
        if (fragment == null){
            fm.beginTransaction().add(R.id.id_left_menu_container,new MenuLeftFragment()).commit();
        }

        mListView = (ListView) findViewById(R.id.id_listview);
        mListView.setAdapter(new ArrayAdapter<String>(this,-1,mDatas){
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

                if (convertView == null){
                    convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item,parent,false);
                }

                TextView tv = convertView.findViewById(R.id.id_tv_title);
                tv.setText(getItem(position));


                return convertView;
            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    private String mSkinPluginPath = Environment.getExternalStorageDirectory()+ File.separator+"skin_plugin.apk";
    private String mSkinPluginPkg = "com.skinchange";

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        switch (id){
            case R.id.action_changeSkin:
                loadPlugin(mSkinPluginPath,mSkinPluginPkg);
                break;

            case R.id.action_testFactory:
                Intent intent = new Intent(this,TestFactoryActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }




    private void loadPlugin(String skinPluginPath, String skinPluginPkg) {

        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPathMethod = assetManager.getClass().getMethod("addAssetPath",String.class);
            addAssetPathMethod.invoke(assetManager,skinPluginPath);

            Resources superResources = getResources();
            Resources resources = new Resources(assetManager,superResources.getDisplayMetrics(),superResources.getConfiguration());

            ResourcesManager resourcesManager = new ResourcesManager(resources,skinPluginPkg);
            Drawable drawable = resourcesManager.getDrawableByResName("skin_main_bg");

            if (drawable != null){
                mDrawerLayout.setBackgroundDrawable(drawable);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
